package com.qingsong.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qingsong.dto.PageDTO;
import com.qingsong.dto.PermissionInsertDTO;
import com.qingsong.dto.PermissionUpdateDTO;
import com.qingsong.dto.PermissionPageDTO;
import com.qingsong.entity.Permission;
import com.qingsong.excel.PermissionExcel;
import com.qingsong.service.PermissionService;
import com.qingsong.util.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qingsong
 */
@Tag(name="权限模块")
@RestController
@RequestMapping("/api/v1/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @Operation(summary = "单增一条记录")
    @PostMapping("save")
    public Object save(@RequestBody @Validated PermissionInsertDTO permissionInsertDTO){
        return permissionService.insert(permissionInsertDTO);
    }

    @Operation(summary = "单查一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return permissionService.selectById(id);
    }

    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated PermissionUpdateDTO permissionUpdateDTO){
        return permissionService.updateById(permissionUpdateDTO);
    }

    @Operation(summary = "删除一条记录")
    @PostMapping ("/remove/{id}")
    public Object remove(@PathVariable Long id){
        return permissionService.logicDeleteById(id);
    }

    @Operation(summary = "批量删除多条记录")
    @PostMapping ("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){
        return permissionService.logicDeleteByIds(ids);
    }

    @Operation(summary = "查询全部记录")
    @GetMapping ("list")
    public Object list(){
        return permissionService.list();
    }

    @Operation(summary = "分页查询记录")
    @PostMapping ("page")
    public Object page(@RequestBody @Validated PageDTO pageDTO){
        return permissionService.page(pageDTO);
    }

    @Operation(summary = "打印数据报表")
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<PermissionExcel> permissionExcels = new ArrayList<>();
        List<Permission> permissions = permissionService.list();
        for (Permission permission : permissions) {
            PermissionExcel permissionExcel = BeanUtil.copyProperties(permission, PermissionExcel.class);
            Long pid = permission.getPid();
            if(pid == 0){
                permissionExcel.setParentTitle("无");
            }else {
                permissionExcel.setParentTitle(permission.getParentPermission().getTitle());
            }
            permissionExcels.add(permissionExcel);
        }
        EasyExcelUtil.download(resp, "权限统计表.xlsx", "sheet01",
                PermissionExcel.class, permissionExcels);
    }

    @Operation(summary = "按权限名搜索")
    @PostMapping("pageLikeTitle")
    public Object pageLikeTitle(@RequestBody @Validated PermissionPageDTO dto){
        return permissionService.pageLikeTitle(dto);
    }

    @Operation(summary = "查询父权限")
    @GetMapping ("listParents")
    public Object listParents(){
        return permissionService.listParents();
    }
}
